package com.ecloud.common.data.handler;

import javax.servlet.http.HttpServletRequest;

import org.aspectj.lang.JoinPoint;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

/**
 * Session处理器 第二个处理器
 *
 * @author LiuGangQiang Create in 2021/04/30
 */
public class DSSessionHandler extends DynamicDSHandler {
    private static final String SESSION_PREFIX = "#session.";

    /**
     * @see com.ecloud.common.data.handler.DynamicDSHandler#matches(java.lang.String)
     */
    @Override
    public boolean matches(String key) {
        return key.startsWith(SESSION_PREFIX);
    }

    /**
     * @see com.ecloud.common.data.handler.DynamicDSHandler#analysis(org.aspectj.lang.JoinPoint,java.lang.String)
     */
    @Override
    public String analysis(JoinPoint point, String key) {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        Object obj = request.getSession().getAttribute(key.substring(9));
        return obj == null ? null : obj.toString();
    }
}